﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;
using QuanLyPhongVePhim.DataAccess;

namespace QuanLyPhongVePhim.Classes
{
    public class NhanVien
    {
        #region Properties

        public int MaNhanVien { get; set; }

        public string Ho { get; set; }

        public string Ten { get; set; }
        
        public string DiaChi { get; set; }

        public string DienThoai { get; set; }

        public string GioiTinh { get; set; }
        
        public string Cmnd { get; set; }
        
        public DateTime? NgayVaoLam { get; set; }
        
        public string LoaiNhanVien { get; set; }

        #endregion


        #region public methods

        public bool DeleteEmployee()
        {
            bool result = false;
            string strSql=""; 
            strSql = string.Format("DELETE FROM [dbo].[NhanVien]  WHERE [MaNhanVien] ={0}", MaNhanVien);
            DataAccessConnection dataAccess = new DataAccessConnection();
            result = dataAccess.ExecuteNonquery(strSql);
            return result;
        }

        public bool InsertEmployee()
        {
            bool result;

            string strSql = string.Empty;

            strSql =
                string.Format(
                    "INSERT INTO [dbo].[NhanVien]([Ho],[Ten],[DiaChi],[DienThoai],[GioiTinh],[CMND],[NgayVaoLam],[LoaiNhanVien])VALUES (N'{0}',N'{1}',N'{2}',N'{3}',N'{4}',N'{5}',CONVERT(DATETIME, '{6}', 105),N'{7}')",
                    Ho, Ten, DiaChi, DienThoai, GioiTinh, Cmnd, NgayVaoLam, LoaiNhanVien);
            DataAccessConnection dataAccess = new DataAccessConnection();
            result = dataAccess.ExecuteNonquery(strSql);

            return result;
        }

        public bool UpdateEmployee()
        {
            bool result;

            string strSQL = string.Empty;

            strSQL =
                string.Format(
                    "UPDATE [dbo].[NhanVien] SET [Ho] = N'{0}' ,[Ten] = N'{1}' ,[DiaChi] = N'{2}' ,[DienThoai] = N'{3}' ,[GioiTinh] = N'{4}',[CMND] = N'{5}',[NgayVaoLam] = CONVERT(DATETIME, '{6}', 105),[LoaiNhanVien] = N'{7}' WHERE [MaNhanVien] = {8}",
                    Ho, Ten, DiaChi, DienThoai, GioiTinh, Cmnd, NgayVaoLam, LoaiNhanVien, MaNhanVien);
            DataAccessConnection dataAccess = new DataAccessConnection();
            result = dataAccess.ExecuteNonquery(strSQL);

            return result;
        }


        public bool UpdateEmployeeInfomation()
        {
            if (MaNhanVien > 0)
                return UpdateEmployee();
            else
            {
                return InsertEmployee();
            }
        }

        public DataTable GetEmployeeList()
        {

            DataTable ds = new DataTable();
            string strSQl = "SELECT *   FROM NhanVien ORDER BY [Ho]";
            DataAccessConnection dataConnect = new DataAccessConnection();
            ds = dataConnect.ExecuteReader(strSQl);
            return ds;
        }

        public void GetEmppoyeeInfo()
        {
            DataTable ds = new DataTable();
            string sql = string.Format("SELECT *   FROM NhanVien Where MaNhanVien={0}", MaNhanVien);
            DataAccessConnection dataAccess = new DataAccessConnection();
            ds = dataAccess.ExecuteReader(sql);
            if (ds.Rows.Count > 0)
            {
                DataRow row = ds.Rows[0];
                Ho = row["Ho"].ToString();
                Ten = row["Ten"].ToString();
                DiaChi = row["DiaChi"].ToString();
                DienThoai = row["DienThoai"].ToString();
                GioiTinh = row["GioiTinh"].ToString();
                Cmnd = row["CMND"].ToString();
                LoaiNhanVien = row["LoaiNhanVien"].ToString();
                DateTime dt = new DateTime();
                if (DateTime.TryParse(row["NgayVaoLam"].ToString(), out dt))
                    NgayVaoLam = dt;

            }
        }


        #endregion
       
    }
}
    